home *** CD-ROM | disk | FTP | other *** search
- /*
- cvcolor.cpp
-
- Custom color control
-
- C++/Views 2.0 Demo
- Copyright (c) 1992, by Liant Software Corp.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- */
-
- #include "cvcolor.h"
- #include "notifier.h"
- #include "port.h"
- #include "brush.h"
- #include "pen.h"
-
- defineClass(ColorControl, VControl)
-
- rgbColor colors[16];
-
- ColorControl::ColorControl()
- {
- ;
- }
-
- ColorControl::ColorControl(VFrame &frame, VWindow *parent, short style)
- : VControl(frame, parent, style)
- {
- currColor = 4;
- clickMthd = 0;
-
- // initialize colors
- colors[ 0] = RGBCLR( 0, 0, 0);
- colors[ 1] = RGBCLR(128, 0, 0);
- colors[ 2] = RGBCLR( 0, 128, 0);
- colors[ 3] = RGBCLR( 0, 0, 128);
- colors[ 4] = RGBCLR(128, 128, 0);
- colors[ 5] = RGBCLR(128, 0, 128);
- colors[ 6] = RGBCLR( 0, 128, 128);
- colors[ 7] = RGBCLR(128, 128, 128);
- colors[ 8] = RGBCLR( 96, 96, 96);
- colors[ 9] = RGBCLR(255, 0, 0);
- colors[10] = RGBCLR( 0, 255, 0);
- colors[11] = RGBCLR( 0, 0, 255);
- colors[12] = RGBCLR(255, 255, 0);
- colors[13] = RGBCLR(255, 0, 255);
- colors[14] = RGBCLR( 0, 255, 255);
- colors[15] = RGBCLR(255, 255, 255);
- }
-
- ColorControl::~ColorControl()
- {
- ;
- }
-
- boolean ColorControl::free()
- {
- delete this;
- return(TRUE);
- }
-
- boolean ColorControl::mouseDn(int mx, int my)
- /*
- mouse button pressed in the control
- */
- {
- int clr;
-
- notifier->mouseTracking(TRUE);
- notifier->captureMouseFor(this);
-
- if (getMouseColor(mx, my, &clr)) {
- setColor(clr);
- }
-
- return(TRUE);
- }
-
- boolean ColorControl::mouseMv(int mx, int my, int bStat)
- /*
- Called when the mouse moves pressed in our window
- (and mouse tracking is turned on).
- */
- {
- int clr;
-
- if (bStat && getMouseColor(mx, my, &clr)) {
- setColor(clr);
- }
-
- return(TRUE);
- }
-
- boolean ColorControl::mouseUp(int mx, int my)
- /*
- mouse button released in the control
- */
- {
- int clr;
-
- if (getMouseColor(mx, my, &clr)) {
- setColor(clr);
- }
-
- /* invoke callback method */
- if (client && clickMthd != NIL_METHOD) {
- client->perform(clickMthd);
- }
-
- notifier->mouseTracking(FALSE);
- notifier->releaseCapture();
- return(TRUE);
- }
-
- boolean ColorControl::getMouseColor(int mx, int my, int *clr)
- /*
- Determine which color cell the mouse is pointing to.
- This function is used by the mouse routines.
- */
- {
- int x, y, w, h;
- int row, col;
- getArea(&x, &y, &w, &h);
-
- row = my / (h / 2);
- col = mx / (w / 8);
-
- *clr = (row * 8) + col;
-
- return(TRUE);
- }
-
- void ColorControl::setColor(int clr)
- /*
- Highlight a new cell and update the window.
- */
- {
- int oldclr = currColor;
- currColor = clr;
-
- if (oldclr != currColor) {
- int x, y, w, h;
- int row, col;
- getArea(&x, &y, &w, &h);
-
- VRectangle drawbox;
-
- /* repaint old cell */
- col = oldclr % 8;
- row = oldclr / 8;
- drawbox.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
- update(&drawbox);
-
- /* repaint new cell */
- col = currColor % 8;
- row = currColor / 8;
- drawbox.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
- update(&drawbox);
- }
- }
-
- boolean ColorControl::paint()
- /*
- Draw the color control.
- */
- {
- VPen pn;
- VBrush br;
- VPort p(this);
-
- p.open();
- p.usePen(&pn);
- p.useBrush(&br);
-
- //draw color rectangles
- int x, y, w, h;
- int row, col;
- getArea(&x, &y, &w, &h);
-
- VRectangle r;
-
- for (row = 0; row < 2; row++) {
- for (col = 0; col < 8; col++) {
-
- r.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
-
- if ((row * 8 + col) == currColor) {
- /* highlight current color */
- pn.color((currColor == 15) ? RED : WHITE);
- }
- else {
- pn.color(BLACK);
- }
-
- br.foreground(colors[row * 8 + col]);
-
- p.fillRegion(&r);
- }
- }
-
- p.useFont(NIL);
- p.usePen(NIL);
- p.close();
-
- return(TRUE);
- }
-
- boolean ColorControl::erased()
- /*
- Intercept painting of window background.
- */
- {
- return(TRUE);
- }
-
- rgbColor ColorControl::getColor()
- /*
- Return the currently selected color value
- */
- {
- return(colors[currColor]);
- }
-
- void ColorControl::uponClick(id clnt, method mthd)
- {
- if (clnt) {
- client = clnt;
- }
- if (mthd) {
- clickMthd = mthd;
- }
- if (!clnt || mthd == NIL_METHOD) {
- client = NIL;
- clickMthd = NIL;
- }
- }
-
-